# Analyze Starbucks locations and plot in a shapefile
import matplotlib.image as image
import geopandas as gpd
import descartes
import pandas as pd
from shapely.geometry import Point, Polygon
import matplotlib.pyplot as plt
%matplotlib inline
# Create a pandas dataframe from a CSV file
df = pd.read_csv('shapefiles/starbucksdirectory.csv')
# Display first 5 rows
df.head()
# Use pandas groupby to view which cities have the most Starbucks locations
df2 = df.groupby('City').Brand.agg('count').to_frame('Count').reset_index()
df2.sort_values('Count').tail(15).sort_values(by='Count', ascending=False)
print ("Total Starbucks in the State of Washington =", df[df['State/Province'] == 'WA'].shape[0])
print('-------------------------')
print ('Total Starbucks in the State of Texas=', df[df['State/Province'] == 'TX'].shape[0])
# Open shapefile
seattle_map = gpd.read_file('shapefiles/Seattle_Streets.shp')
fig,ax = plt.subplots(figsize = (15,15))
# Display Seattle, WA using matplotlib
seattle_map.plot(ax = ax)
# Create new geometry dataframe adding new 'geometry' column
geometry = [Point(xy) for xy in zip(df["Longitude"], df["Latitude"])]
crs = {'init' : 'epsg:4326'}
geo_df = gpd.GeoDataFrame(df, #specify our data
crs = crs, #specify our coordinate reference system
geometry = geometry) #specify the geomtry list we created
geo_df.head()
# Displaying the Seattle map with Starbucks locations per CSV file (geo_df)
# The lat and long don't have enough sig figs to yield significant results
fig,ax = plt.subplots(figsize = (30,30))
seattle_map.plot(ax = ax, alpha = 0.4, color = "grey")
geo_df[geo_df['City'] == 'Seattle'].plot(ax = ax, markersize = 50, color = "green", marker = "^", label = "Starbs Coffee")
plt.legend(prop={'size':15})
# San Antonio Map. Same process as above
shp_location = '/Users/emilybecker/Downloads/Streets-shp'
sa_map = gpd.read_file(shp_location)
fig,ax = plt.subplots(figsize = (45,45))
sa_map.plot(ax = ax)
# Plot Starbucks locations in San Antonio
fig,ax = plt.subplots(figsize = (30,30))
sa_map.plot(ax = ax, alpha = 0.4, color = "grey")
geo_df[geo_df['City'] == 'San Antonio'].plot(ax = ax, markersize = 50, color = "green", marker = "^", label = "Coffee")
plt.legend(prop={'size':15})